package net.liteheaven.xblib.plugin;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

/**
 * 插件代理Activity基类
 *
 * 写插件的时候禁止直接在Activity中覆写方法，在controller中实现
 *
 * Created by LiangYu on 16/8/29
 */
public class XProxyActivity extends Activity implements IHasDad, IXActivityController {

    private XPlugin mXPlugin;
    private DadImpl mDad;
    private IXActivityController mLifeCycle;

    private Resources.Theme mTheme; // 托管插件的样式
    private Resources mResources; // 托管插件的资源
    private AssetManager mAssetManager; // 托管插件的assets

    public XProxyActivity(){
        super();
    }

    @Override
    public AssetManager getAssets() {
        return mAssetManager == null ? super.getAssets() : mAssetManager;
    }

    @Override
    public Resources getResources() {
        return mResources == null ? super.getResources() : mResources;
    }

    @Override
    public Resources.Theme getTheme() {
        return mTheme == null ? super.getTheme() : mTheme;
    }

    private void initPlugin(){
        mDad = new DadImpl();
        mXPlugin = XPluginLoadHelper.getPlugin(this);
        if (mXPlugin != null) {
            mLifeCycle = mXPlugin.getController(this);
            mAssetManager = mXPlugin.getAssetManager(super.getAssets());
            if (mAssetManager != null){
                Resources res = super.getResources();
                mResources = new Resources(mAssetManager, res.getDisplayMetrics(), res.getConfiguration());
            }
            if (mResources != null){
                mTheme = mResources.newTheme();
                mTheme.setTo(super.getTheme());
            }
        }else{
            // 静态动态都失败，保底controller
            mLifeCycle = getController(this);
        }
    }

    @Override
    final public void onCreate(Bundle savedInstanceState) {
        initPlugin();
        super.onCreate(savedInstanceState);

        mLifeCycle.onCreate(savedInstanceState);
    }

    @Override
    final public void onDestroy() {
        mLifeCycle.onDestroy();
    }

    @Override
    final public void onResume() {
        mLifeCycle.onResume();
    }

    @Override
    final public void onPause() {
        mLifeCycle.onPause();
    }

    @Override
    final public void onStart() {
        mLifeCycle.onStart();
    }

    @Override
    final public void onStop() {
        mLifeCycle.onStop();
    }

    @Override
    final public void onRestart() {
        mLifeCycle.onRestart();
    }

    @Override
    final public boolean dispatchTouchEvent(MotionEvent ev) {
        return mLifeCycle.dispatchTouchEvent(ev);
    }

    @Override
    final public boolean onTouchEvent(MotionEvent event){
        return mLifeCycle.onTouchEvent(event);
    }

    @Override
    final public void onBackPressed() {
        mLifeCycle.onBackPressed();
    }

    @Override
    final public void onActivityResult(int requestCode, int resultCode, Intent data) {
        mLifeCycle.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    final public void onNewIntent(Intent intent) {
        mLifeCycle.onNewIntent(intent);
    }

    @Override
    final public void onSaveInstanceState(Bundle outState) {
        mLifeCycle.onSaveInstanceState(outState);
    }

    @Override
    final public void onRestoreInstanceState(Bundle savedInstanceState) {
        mLifeCycle.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    final public void onWindowFocusChanged(boolean hasFocus) {
        mLifeCycle.onWindowFocusChanged(hasFocus);
    }

    @Override
    final public void onConfigurationChanged(Configuration newConfig) {
        mLifeCycle.onConfigurationChanged(newConfig);
    }

    @Override
    final public boolean onKeyDown(int keyCode, KeyEvent event) {
        return mLifeCycle.onKeyDown(keyCode, event);
    }

    @Override
    final public boolean onKeyUp(int keyCode, KeyEvent event) {
        return mLifeCycle.onKeyUp(keyCode, event);
    }

    /**
     * 插件程序员负责覆写此方法
     * @return
     */
    public IXActivityController getController(XProxyActivity activity){
        return new XActivityController(activity);
    }

    final public IXActivityImpl getDad(){
        return mDad;
    }

    /**
     * 父类方法集合
     */
    private class DadImpl implements IXActivityImpl {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            XProxyActivity.super.onCreate(savedInstanceState);
        }

        @Override
        public void onDestroy() {
            XProxyActivity.super.onDestroy();
        }

        @Override
        public void onResume() {
            XProxyActivity.super.onResume();
        }

        @Override
        public void onPause() {
            XProxyActivity.super.onPause();
        }

        @Override
        public void onStart() {
            XProxyActivity.super.onStart();
        }

        @Override
        public void onStop() {
            XProxyActivity.super.onStop();
        }

        @Override
        public void onRestart() {
            XProxyActivity.super.onRestart();
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            return XProxyActivity.super.dispatchTouchEvent(ev);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            return XProxyActivity.super.onTouchEvent(event);
        }

        @Override
        public void onBackPressed() {
            XProxyActivity.super.onBackPressed();
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            XProxyActivity.super.onActivityResult(requestCode, resultCode, data);
        }

        @Override
        public void onNewIntent(Intent intent) {
            XProxyActivity.super.onNewIntent(intent);
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            XProxyActivity.super.onSaveInstanceState(outState);
        }

        @Override
        public void onRestoreInstanceState(Bundle savedInstanceState) {
            XProxyActivity.super.onRestoreInstanceState(savedInstanceState);
        }

        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            XProxyActivity.super.onWindowFocusChanged(hasFocus);
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            XProxyActivity.super.onConfigurationChanged(newConfig);
        }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            return XProxyActivity.super.onKeyDown(keyCode, event);
        }

        @Override
        public boolean onKeyUp(int keyCode, KeyEvent event) {
            return XProxyActivity.super.onKeyUp(keyCode, event);
        }

        @Override
        public void finish() {
            XProxyActivity.super.finish();
        }

        @Override
        public void setContentView(View view) {
            XProxyActivity.super.setContentView(view);
        }

        @Override
        public void setContentView(int layoutResID) {
            XProxyActivity.super.setContentView(layoutResID);
        }

        @Override
        public View findViewById(int id) {
            return XProxyActivity.super.findViewById(id);
        }

        @Override
        public void attachBaseContext(Context newBase) {
            XProxyActivity.super.attachBaseContext(newBase);
        }
    }
}
